﻿using Newtonsoft.Json;
using WMS.FC.Domain.ValueObject.System;

namespace WMS.FC.Domain.DomainObject.System
{
    /// <summary>
    /// 响应基类
    /// </summary>
    public class ResponseBase
    {
        /// <summary>
        /// 响应代码
        /// </summary>
        [JsonProperty("Code")]
        public string Code { get; set; } = "S";

        /// <summary>
        /// 响应消息
        /// </summary>
        [JsonProperty("Msg")]
        public string Message { get; set; } = string.Empty;

        /// <summary>
        /// 响应数据
        /// </summary>
        [JsonProperty("Data")]
        public object? Data { get; set; } = null;

        /// <summary>
        /// 程序抛出异常
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public ResponseBase Exception(Exception ex)
        {
            Code = ErrorCode.E006.ToString();
            Message = ex.Message;
            return this;
        }

        /// <summary>
        /// 传递结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public ResponseBase TransferResult(ActionResultBase result)
        {
            Code = result.ErrorCode.ToString();
            Message = result.Message;
            return this;
        }

        /// <summary>
        /// 传递结果
        /// </summary>
        /// <param name="result"></param>
        /// <returns></returns>
        public ResponseBase TransferResult(ActionResult<string> result)
        {
            Code = result.ErrorCode.ToString();
            Message = result.Message;
            Data = result.Data;
            return this;
        }

        /// <summary>
        /// 传递结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="result"></param>
        /// <param name="map"></param>
        /// <returns></returns>
        public ResponseBase TransferResult<T, S>(ActionResult<T> result, Func<object?, S?> map)
            where T : DomainObjectBase
            where S : ValueObjectBase
        {
            Code = result.ErrorCode.ToString();
            Message = result.Message;
            Data = map(result.Data);
            return this;
        }

        /// <summary>
        /// 传递结果
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="result"></param>
        /// <param name="map"></param>
        /// <returns></returns>
        public ResponseBase TransferResult<T, S>(ActionResult<List<T>> result, Func<object?, List<S>?> map)
            where T : DomainObjectBase
            where S : ValueObjectBase
        {
            Code = result.ErrorCode.ToString();
            Message = result.Message;
            Data = map(result.Data);
            return this;
        }
    }
}
